package org.rendao.biz.web;

import java.util.List;

import org.rendao.auth.model.UserDetail;
import org.rendao.auth.model.UserRole;
import org.rendao.auth.service.UserDetailService;
import org.rendao.auth.service.UserRoleService;
import org.rendao.biz.model.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

	@Autowired
	private UserRoleService roleService;
	
	@Autowired
	private UserDetailService userService;
	
	@RequestMapping(value = "/role/get_all", method = RequestMethod.GET)
	@ResponseBody
	public SearchResult<UserRole> getAllRoles() throws Exception {
		List<UserRole> list = roleService.getAll();
		SearchResult<UserRole> result = SearchResult.byCollection(list);
		return result;
	}
	
	@RequestMapping(value = "/user/get_all", method = RequestMethod.GET)
	@ResponseBody
	public SearchResult<UserDetail> getAllUsers() throws Exception {
		List<UserDetail> list = userService.getAll();
		SearchResult<UserDetail> result = SearchResult.byCollection(list);
		return result;
	}
	
	@RequestMapping(value = "/user/get/{id}", method = RequestMethod.GET)
	@ResponseBody
	public UserDetail getById(@PathVariable("id")String id) throws Exception {
		return userService.getById(id);
	}
	
	@RequestMapping(value = "/user/save", method = RequestMethod.POST)
	@ResponseBody
	public UserDetail saveOrUpdate(@RequestBody UserDetail userDetail) throws Exception {
		return userService.saveOrUpdate(userDetail);
	}
	
	@RequestMapping(value = "/user/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public int deleteById(@PathVariable("id")String id) throws Exception {
		return userService.deleteById(id);
	}
}
